package com.letv.whatslive.mongo.dao;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.ActionLog;
import com.letv.whatslive.model.convert.ActionLogConvert;
import com.letv.whatslive.mongo.BaseDAO;
import com.mongodb.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by gaoshan on 15-9-24.
 */
@Repository
public class ActionLogDAO extends BaseDAO {

    private final static Logger logger = LoggerFactory.getLogger(ActionLogDAO.class);

    protected String collectionName = "actionLog";

    @Override
    protected void init() {
        super.init(this.collectionName);
    }

    @Override
    protected long getAutoIncrementId() {
        return idGenerate.generateId(this.collectionName);
    }

    /**
     * 插入行为日志
     * @param actionLog
     */
    public long insertActionLog(ActionLog actionLog){
        try {
            DBObject obj = ActionLogConvert.castActionLogToDBObject(actionLog);
            return this.insert(obj);
        } catch (Exception e) {
            logger.error("mongodb insert data error! "+collectionName, e);
            return -1L;
        }
    }

    public ActionLog getActionLogById(Long id){
        DBObject dbObject = new BasicDBObject();
        dbObject.put("_id", id);
        DBObject result = dbCollection.findOne(dbObject);
        if (result == null) {
            return null;
        }
        ActionLog actionLog = ActionLogConvert.castDBObjectToActionLog(result);
        return actionLog;
    }

    /**
     * 查询行为日志的记录数
     * @param params 查询条件参数
     * @return 满足条件的文档记录数
     */
    public long countActionLogByParams(Map params) {
        try {
            //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
            BasicDBObject query = getBasicDBObjectByParams(params);
            return this.dbCollection.count(query);
        } catch (Exception e) {
            logger.error("mongodb count data error! "+collectionName, e);
            return -1L;
        }
    }

    /**
     * 查询行为日志字符串列表
     * @param orders 查询的排序条件
     * @param params 模糊查询的查询条件
     * @param start  查询条件的开始分页数
     * @param limit  查询条件的分页每页条数
     * @param programStartTime  直播开始时间，用于计算行为日志的相对时间
     * @param playTime 直播的播放时长，用于计算行为日志的相对时间
     * @param endTime 直播的结束时间，用于计算行为日志的相对时间
     * @return
     */
    public Map<String, List<String>> getActionLogJsonStrListByParams(Map<String, Object> orders, Map<String, Object> params
            , Integer start, Integer limit, long programStartTime,Long playTime,Long endTime) {

        List<String> actionLogJsonList = Lists.newArrayList();
        List<String> userIdList = Lists.newArrayList();
        Map<String, String> userIdMap = Maps.newHashMap();
        if (limit == null) {
            limit = DEFAULT_NUM;
        }
        try {
            //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
            BasicDBObject query = getBasicDBObjectByParams(params);
            BasicDBObject order = getOrderParams(orders);
            Integer skip = start == null ? 0 : start;
            DBCursor cur = this.dbCollection.find(query).skip(skip).sort(order).limit(limit);
            while (cur.hasNext()) {
                DBObject dbObject = cur.next();
                Map<String, Object> value = ActionLogConvert.castDBObjectToMap(dbObject, programStartTime,playTime,endTime);
                actionLogJsonList.add(JSON.toJSONString(value));
                String uid = ObjectUtils.toString(value.get("uid"));
                userIdMap.put(uid,uid);
            }
            userIdList.addAll(userIdMap.values());
        } catch (Exception e) {
            logger.error("mongodb query data list error! "+collectionName, e);
        }
        Map result = Maps.newHashMap();
        result.put("actionLog", actionLogJsonList);
        result.put("userId", userIdList);
        return result;
    }

    public List<ActionLog> getActionLogListByParams(Map<String, Object> params, Map<String, Object> orders, Integer start, Integer limit){
        List<ActionLog> result = Lists.newArrayList();
        if (limit == null) {
            limit = DEFAULT_NUM;
        }
        try {
            //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
            BasicDBObject query = getBasicDBObjectByParams(params);
            BasicDBObject order = getOrderParams(orders);
            Integer skip = start == null ? 0 : start;
            DBCursor cur = this.dbCollection.find(query).sort(order).skip(skip).limit(limit);
            while (cur.hasNext()) {
                DBObject dbObject = cur.next();
                ActionLog actionLog = ActionLogConvert.castDBObjectToActionLog(dbObject);
                result.add(actionLog);
            }
        } catch (Exception e) {
            logger.error("mongodb query data list error! "+collectionName, e);
        }
        return result;
    }

    /**
     * 根据条件查询总的记录数
     * @param params
     * @return
     */
    public Long getActionLogCount(Map<String, Object> params){
        Long count = 0L;
        try {
            BasicDBObject query = getBasicDBObjectByParams(params);
            count = this.dbCollection.count(query);
        } catch (Exception e) {
            logger.error("mongodb query data count error! "+collectionName, e);
        }
        return count;
    }


    /**
     * 根据直播id删除所有直播的回放日志
     * @param pid
     * @return
     */
    public int deleteActionLogById(String pid){
        Map<String, Object> queryParams = Maps.newHashMap();
        queryParams.put("roomId",pid);
        DBObject dbObject = getBasicDBObjectByParams(queryParams);
        WriteResult writeResult = dbCollection.remove(dbObject);
        return writeResult.getN();
    }

    /**
     * 根据查询参数Map获取查询条件对象
     * @param params 查询参数Map
     * @return
     */
    private BasicDBObject getBasicDBObjectByParams(Map params){
        //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
        BasicDBObject query = new BasicDBObject();// 新建查询基类对象 dbo
        if (params != null && params.size() > 0) {
            if (params.get("roomId") != null) {
                query.put("roomId", ObjectUtils.toString(params.get("roomId")));
            }
            if (params.get("action") != null) {
                query.put("action", ObjectUtils.toInteger(params.get("action")));
            }
            if (params.get("content") != null) {
                Pattern pattern = Pattern.compile("^.*" + params.get("content") + ".*$", Pattern.CASE_INSENSITIVE);
                query.put("content", pattern);
            }
            if(params.get("uids")!=null){
                List<Long> ids= (List<Long>) params.get("uids");
                if(ids.size()>0){
                    BasicDBList values = new BasicDBList();
                    for(Long id:ids){
                        values.add(ObjectUtils.toString(id));
                    }
                    query.put("uid", new BasicDBObject("$in", values));
                }
            }
        }
        return query;
    }

    public void deleteActionLogByActionLogId(Long id){
        BasicDBObject query = new BasicDBObject();
        query.put("_id", id);
        this.dbCollection.remove(query);
    }

    /**
     * 获取排序的BasicDBObject对象
     * @param orders
     * @return
     */
    private BasicDBObject getOrderParams(Map<String, Object> orders) {
        BasicDBObject order = new BasicDBObject();
        if(null != orders &&orders.size()>0){
            for(String key : orders.keySet()){
                order.append(key, Integer.valueOf(String.valueOf(orders.get(key))));
            }
        }
        return order;
    }

}
